package com.yu.crm.service.impl;

import cn.hutool.core.io.FileUtil;
import cn.hutool.extra.qrcode.QrCodeUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yu.crm.domain.pojo.CrmDepartment;
import com.yu.crm.domain.pojo.CrmDepartmentMember;
import com.yu.crm.domain.pojo.CrmQrPatrol;
import com.yu.crm.mapper.CrmQrPatrolMapper;
import com.yu.crm.service.CrmDepartmentMemberService;
import com.yu.crm.service.CrmDepartmentService;
import com.yu.crm.service.ICrmQrPatrolService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yu.crm.utils.QRCodeUtil;
import com.yxzz.common.config.UserCacheManager;
import com.yxzz.common.helper.DataHelpers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author pan
 * @since 2020-09-22
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class CrmQrPatrolService extends ServiceImpl<CrmQrPatrolMapper, CrmQrPatrol> implements ICrmQrPatrolService {

    @Autowired
    UserCacheManager userCacheManager;
    @Autowired
    CrmDepartmentMemberService departmentMemberService;
    @Autowired
    QrCreateService qrCreateService;

    @Value("generate_qr_path")
    private  final String GENERATE_QR_PATH = "E:/";

    @Override
    public void insert(CrmQrPatrol crmQrPatrol) throws IOException {
        String maxCodeNumber = queryMaxCodeNumber();
        String[] split = maxCodeNumber.split("-");
        Integer i = Integer.valueOf(split[1]);
        String qr_code_path = GENERATE_QR_PATH + UUID.randomUUID().toString().replace("-","")+".jpg";
        String userId = userCacheManager.getCurrentUser().getUserId();
        CrmDepartmentMember departmentMember = departmentMemberService.infoByUserId(userId);
        crmQrPatrol.setCreateUser(userId);
        crmQrPatrol.setDeptId(departmentMember.getDepartmentId());
        crmQrPatrol.setCodeNumber("QR-"+String.format("%05d",i+1));
        crmQrPatrol.setQrCodePath(qr_code_path);//设置二维码路径
        save(crmQrPatrol);
        //todo 添加微信二维码
        qrCreateService.createQrCode("604800",crmQrPatrol.getId().toString(),qr_code_path);
    }

    @Override
    public CrmQrPatrol detail(Long id) {
        CrmQrPatrol qrPatrol = baseMapper.detail(id);
        return qrPatrol;
    }

    @Override
    public List<CrmQrPatrol> findAll() {
        return baseMapper.findAll();
    }

    @Override
    public void getQrImg(String path, HttpServletResponse response) {
        File file = new File(path);
        try {
            InputStream in = new FileInputStream(file);
            byte[] b = new byte[in.available()];
            in.read(b);
            OutputStream out = response.getOutputStream();
            out.write(b);
            out.flush();
            out.close();
            in.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public String queryMaxCodeNumber() {
        if (DataHelpers.isNotEmpty(baseMapper.queryMaxCodeNumber())) {
           return baseMapper.queryMaxCodeNumber();
        }
        return "QR-"+String.format("%05d",0);
    }
}
